/*
 * pps_bom
 */
package com.pitop.project.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.project.entity.ProjectBom;
import com.pitop.project.service.ProjectBomSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目bom关联 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/projectBom")
@Slf4j
@Api(value = "项目bom关联控制器", description = "项目bom关联控制器")
public class ProjectBomCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private ProjectBomSVImpl projectBomSVImpl;

    /**
     * 根据条件projectCode查询ProjectBom一个详情信息
     *
     * @param projectCode 项目编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProjectBom一个详情信息", notes = "查询ProjectBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/projectCode/{projectCode}")
    @ResponseBody
    public BeanRet loadByProjectCode(@PathVariable String projectCode) {
        if (StringUtils.isEmpty(projectCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProjectBom projectBom = projectBomSVImpl.loadByProjectCode(projectCode);
        log.info(JSON.toJSONString(projectBom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectBom);
    }


    /**
     * 根据条件projectCode查询ProjectBom一个详情信息
     *
     * @param projectCode 项目编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProjectBom一个详情信息", notes = "查询ProjectBom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/projectCode/{projectCode}")
    @ResponseBody
    public BeanRet getByProjectCode(@PathVariable String projectCode) {
        if (StringUtils.isEmpty(projectCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProjectBom projectBom = projectBomSVImpl.getByProjectCode(projectCode);
        log.info(JSON.toJSONString(projectBom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectBom);
    }


    /**
     * 查询ProjectBom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProjectBom信息集合", notes = "查询ProjectBom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ProjectBom projectBom, @ApiIgnore Page<ProjectBom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<ProjectBom> projectBoms = projectBomSVImpl.list(projectBom, page.getCurPage(), page.getPageSize());
        int total = projectBomSVImpl.count(projectBom);
        page.setTotalRow(total);
        page.setVoList(projectBoms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 创建ProjectBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建ProjectBom", notes = "创建ProjectBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码,多个以逗号“,”分割开", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore ProjectBom projectBom) {
        if (StringUtils.isEmpty(projectBom.getProjectCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(projectBom.getBomCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //检测是否重复
        String[] bomCodeAry = projectBom.getBomCode().split(",");
        for (String bomCode : bomCodeAry) {
            if (projectBomSVImpl.load(null, projectBom.getProjectCode(), bomCode) != null) {
                return BeanRet.create(false, "项目" + projectBom.getProjectCode() + "已关联bom:" + bomCode);
            }
        }
        projectBomSVImpl.save(projectBom);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectBom);
    }

    /**
     * 根据主键删除ProjectBom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除ProjectBom", notes = "删除ProjectBom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String projectCode, String bomCode) {
        if (id == null && StringUtils.isEmpty(projectCode) && StringUtils.isEmpty(bomCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectBomSVImpl.delete(id, projectCode, bomCode);
        return BeanRet.create(true, "删除ProjectBom成功");
    }

}
